import { http } from "@/utils/http";

/** 查询参数 */
export interface InspectionCheckQuery extends BasePageQuery {
  inspectionId?: number;
  inspectionResult?: string;
  deptId?: number;
  checkTermCode?: string;
  checkStandardCode?: string;
  determinationStandard?: string;
  inspectionUserName?: string;
  startTime?: string;
  endTime?: string;
  dutyDeptId?: number;
}

/** 添加/修改命令对象（简化定义，根据需要可补全） */
export interface AddOrUpdateInspectionCheckCommand {
  inspectionCheckId?: number;
  inspectionId: number;
  selfcheckId?: number;
  inspectionUserId?: number;
  inspectionResult?: string;
  inspectionQuestion?: string;
  inspectionDeadlineTime?: string;
  rectificationRequire?: string;
  directCause?: string;
  fundamentalCause?: string;
  correctMeasure?: string;
  correctMeasureDeadlineTime?: string;
  correctMeasureDealTime?: string;
  correctMeasureDealPeople?: string;
  correctMeasureResult?: string;
  preventionMeasure?: string;
  preventionMeasureDeadlineTime?: string;
  preventionMeasureDealTime?: string;
  preventionMeasureDealPeople?: string;
  preventionMeasureResult?: string;
  remark?: string;
  dealType?: string;
  relationId?: number;
  relationType?: string;
  startTime?: string;
  endTime?: string;
  feedback?: string;
  positiveProof?: any;
  checkTermId?: number;
  checkStandardId?: number;
  checkTermCode?: string;
  checkStandardCode?: string;
  checkOpinion?: string;
  checkSign?: string;
  inspectionDeptId?: number;
  status?: number;
  dealResult?: string;

  professional?: string; // 专业
  checkContent?: string; // 检查内容
  checkStandard?: string; // 检查标准
  complianceCriteria?: string; // 符合性判定标准
  checkMethod?: string; // 检查方式
  companyBasis?: string; // 公司依据
  companyClause?: string; // 公司条款
  lawsBasis?: string; // 法律法规依据
  lawsClause?: string; // 法律法规条款
  baseCycle?: string; // 基础周期
  integrationSource?: string; // 融合来源
}

/** DTO 对象 */
export interface InspectionCheckDTO {
  inspectionCheckId: number;
  inspectionId: number;
  selfcheckId: number | null;
  inspectionUserId: number | null;
  inspectionResult: string;
  inspectionQuestion: string | null;
  inspectionDeadlineTime: string | null;
  rectificationRequire: string | null;
  directCause: string | null;
  fundamentalCause: string | null;
  correctMeasure: string | null;
  correctMeasureDeadlineTime: string | null;
  correctMeasureDealTime: string | null;
  correctMeasureDealPeople: string | null;
  correctMeasureResult: string | null;
  preventionMeasure: string | null;
  preventionMeasureDeadlineTime: string | null;
  preventionMeasureDealTime: string | null;
  preventionMeasureDealPeople: string | null;
  preventionMeasureResult: string | null;
  remark: string | null;
  dealType: string | null;
  relationId: number | null;
  relationType: string | null;
  startTime: string;
  endTime: string;
  feedback: string | null;
  checkTermId: number | null;
  checkStandardId: number | null;
  checkTermCode: string;
  checkOpinion: string | null;
  checkSign: string | null;
  inspectionDeptId: number | null;
  status: number | null;
  dealResult: string | null;
  createTime: string | null;

  // 新增字段
  inspectionUserName: string | null;
  inspectionName: string | null;
  inspectionDeptName: string | null;
  determinationStandard: string | null;

  // 自查事项库字段
  professional: string | null;
  checkContent: string | null;
  checkStandardCode: string;
  checkStandard: string | null;
  complianceCriteria: string | null;
  checkMethod: string | null;
  companyBasis: string | null;
  companyClause: string | null;
  lawsBasis: string | null;
  lawsClause: string | null;
  baseCycle: string | null;
  integrationSource: string | null;
  checkDepartments: string | null;
  [key: string]: any;
}

/** 分页结果 */
export interface PageDTO<T> {
  rows: T[];
  total: number;
}

/** 通用响应结构 */
export interface ResponseDTO<T> {
  code: number;
  msg: string;
  data: T;
}

/** 获取检查项列表（分页） */
export function getInspectionCheckListApi(
  params: InspectionCheckQuery
): Promise<ResponseDTO<PageDTO<InspectionCheckDTO>>> {
  return http.request("get", "/inspection/check/list", { params });
}

/** 获取单条详情 */
export function getInspectionCheckInfoApi(
  id: number
): Promise<ResponseDTO<InspectionCheckDTO>> {
  return http.request("get", `/inspection/check/${id}`);
}

/** 添加检查项 */
export function addInspectionCheckApi(
  data: AddOrUpdateInspectionCheckCommand
): Promise<ResponseDTO<void>> {
  return http.request("post", "/inspection/check", { data });
}

/** 修改检查项 */
export function updateInspectionCheckApi(
  data: AddOrUpdateInspectionCheckCommand
): Promise<ResponseDTO<void>> {
  return http.request("put", "/inspection/check", { data });
}

/** 删除检查项（批量） */
export function deleteInspectionCheckApi(
  ids: number[]
): Promise<ResponseDTO<void>> {
  const params = new URLSearchParams();
  ids.forEach(id => params.append('ids', id.toString()));

  return http.request("delete", `/inspection/check?${params.toString()}`);
}

/** 导出检查项为 Excel */
export function exportInspectionCheckApi(
  params: InspectionCheckQuery,
  fileName: string
) {
  return http.download("/inspection/check/excel", fileName, {
    params
  });
}

/** 开始检查命令对象 */
export interface UpdateInspectionCheckCommand {
  // 可根据实际需求补充字段，这里先沿用 AddOrUpdateInspectionCheckCommand 的字段
  [key: string]: any;
}

/** 开始检查 */
export function startInspectionApi(
  data: UpdateInspectionCheckCommand
): Promise<ResponseDTO<void>> {
  return http.request("post", "/inspection/check/startInspection", { data });
}

